<?php
namespace Index\Controller;
use Think\Controller;
use Think\Verify;
class RegisterController extends Controller {
    public function indexAction(){
        if(IS_POST){
            $username = I('post.userName');
            $password = I('post.password');
            $phone = I('post.mobile');
            $checkPasswd =I('post.checkPasswd');
            $verify = I('post.seccode_verify');
            $pro =  I('post.pro');

            if(strlen($username)<2) $this->error('用户名少于2位','',2);
            if(strlen($password)<6 || strlen($checkPasswd)<6) $this->error('密码少于6位','',2);
            if($password!=$checkPasswd) $this->error('密码不一致','',2);
            $password = md5($password);
            $checkPasswd = md5($checkPasswd);

            //校对验证码
            if(!$this->check_verify($verify)) $this->error('验证码不正确','',2);

            if($pro!=9) $this->error('请先接受酷学网注册条款','',2);

            // 判断名字是否存在
            $Model = M();
            $isExist = $Model->query("select * from user where username='{$username}'");
            if (!empty($isExist)) $this->error('用户已存在','',2);


            $registerTime = time();
            $face = "/Public/Images/default_img_1.jpg";

            $sql = "insert into user(username,password,phone,registerTime,face) values('{$username}','{$password}','{$phone}','{$registerTime}','{$face}')";

            if($Model->execute($sql)){
                $this->success('注册成功',U('Index/Login/index'),1);
            }


        }else{
            $this->display('index');
        }
    }


    /**
    生成验证码类
     **/
    public function CodeAction(){
        $config = array(
            'imageW'=>104,
            'imageH'=>32,
            'fontSize'=>16,
            'length'=>4,
            'bg'=>array(204, 203, 221),
            'useCurve' =>false            // 是否画混淆曲线
        );
        $Verify = new Verify($config);
        $Verify->entry();
    }

    /**
    验证用户提交的验证码
     **/
    public function check_verify($code, $id = ''){
        $verify = new Verify();
        return $verify->check($code, $id);
    }
}